package student.aistar.boxSeckillTask;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import student.aistar.model.entity.Orderdetail;
import student.aistar.model.entity.Orders;
import student.aistar.model.entity.User;
import student.aistar.service.IBoxSeckillService;
import student.aistar.service.IPersonalCenterService;
import student.aistar.service.IProductService;
import student.aistar.service.IUserService;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @Description:小米音箱秒杀订单生成
 * @Auther: zhugefeng
 * @Date:2021/10/16 10:33
 **/
@Component
public class BoxSeckillOrdeRbm {
    @Autowired
    private IBoxSeckillService boxSeckillService;

    @Autowired
    private IPersonalCenterService personalCenterService;

    @Autowired
    private IProductService productService;

    @RabbitListener(queues = "boxSeckillQue")
    @RabbitHandler
    public void getBoxSeckillOrder(Map<String, Object> map, @Headers Map<String, Object> header, Channel channel) {
        System.out.println("map:" + map);


        String uName = (String) map.get("username");
        long killId = (long) map.get("killId");

        Integer userId = personalCenterService.selectByName(uName).getUserId();
        String orderNumber = new Date().getTime()+""+userId;
        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setOrderNumber(Long.valueOf(orderNumber));
        orders.setOrderAmount(boxSeckillService.selectBoxSeckillById(killId).getSeckillprice());
        orders.setPayAmount(boxSeckillService.selectBoxSeckillById(killId).getSeckillprice());



        boxSeckillService.saveBoxSeckillOrder(UUID.randomUUID().toString(), uName, killId);
        boxSeckillService.insertOrders(orders);
        Orderdetail orderdetail = new Orderdetail();
        orderdetail.setOrderId(orders.getOrderId());
        orderdetail.setProductId(boxSeckillService.selectBoxSeckillById(killId).getProductid());
        orderdetail.setName(productService.findById(boxSeckillService.selectBoxSeckillById(killId).getProductid()).getProductName());
        orderdetail.setPicImg(productService.findById(boxSeckillService.selectBoxSeckillById(killId).getProductid()).getPicImg());
        orderdetail.setProductSpecName(productService.findById(boxSeckillService.selectBoxSeckillById(killId).getProductid()).getProductName());
        orderdetail.setPrice(boxSeckillService.selectBoxSeckillById(killId).getSeckillprice().doubleValue());
        orderdetail.setProductAmount(boxSeckillService.selectBoxSeckillById(killId).getSeckillprice().doubleValue());
        boxSeckillService.insertOrderDetail(orderdetail);

        //手动删除rabbitmq队列中的数据
        long ack = (long) header.get(AmqpHeaders.DELIVERY_TAG);
        try {
            channel.basicAck(ack, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
